package com.yuncheng.oaApi.controller;

import java.util.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.yuncheng.vo.HttpResult;
import com.yuncheng.common.query.QueryBuilder;
import com.yuncheng.annotation.AutoLog;
import com.yuncheng.utils.I18nUtil;
import com.yuncheng.common.base.controller.BaseController;
import com.yuncheng.oaApi.entity.OaSysRole;
import com.yuncheng.oaApi.service.IOaSysRoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.yuncheng.system.api.SystemAPI;
import org.springframework.context.annotation.Lazy;

/**
 * @Description: sys_role
 * @Author: yuncheng
 * @Date: 2023-10-10
 * @Version: V1.0
 */
@Api(tags = "sys_role接口")
@RestController
@RequestMapping("/api/role/sysRole")
public class OaSysRoleController extends BaseController<OaSysRole, IOaSysRoleService> {

    private static final Logger log = LoggerFactory.getLogger(OaSysRoleController.class);

    @Resource
    private IOaSysRoleService sysRoleService;

    @Resource
    @Lazy
    private SystemAPI systemAPI;

    /**
     * 分页列表查询
     *
     * @param oaSysRole
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "sys_role-分页列表查询")
    @ApiOperation(value = "sys_role-分页列表查询")
    @GetMapping(value = "/list")
    public HttpResult<?> queryPageList(OaSysRole oaSysRole, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        QueryWrapper<OaSysRole> queryWrapper = QueryBuilder.initQueryWrapper(oaSysRole, req.getParameterMap());
        Page<OaSysRole> page = new Page<>(pageNo, pageSize);
        IPage<OaSysRole> pageList = sysRoleService.page(page, queryWrapper);
        for(OaSysRole vo:pageList.getRecords()){
            formatValue(vo);
        }
        return HttpResult.ok(pageList);
    }



    /**
     * 添加
     *
     * @param oaSysRole
     * @return
     */
    @AutoLog(value = "sys_role-添加")
    @ApiOperation(value = "sys_role-添加")
    //@RequiresPermissions("role:SysRole:add")
    @PostMapping(value = "/add")
    public HttpResult<?> add(@RequestBody OaSysRole oaSysRole) {
        HttpResult<String> result = new HttpResult(true);
        sysRoleService.save(oaSysRole);
        result.setResult(oaSysRole.getId());
        result.setMessage(I18nUtil.message("save.success"));
        return result;
    }

    /**
     * 编辑
     *
     * @param oaSysRole
     * @return
     */
    @AutoLog(value = "sys_role-编辑")
    @ApiOperation(value = "sys_role-编辑")
    //@RequiresPermissions("role:SysRole:edit")
    @PutMapping(value = "/edit")
    public HttpResult<?> edit(@RequestBody OaSysRole oaSysRole) {
        sysRoleService.updateById(oaSysRole);
        return HttpResult.ok(I18nUtil.message("update.success"));
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "sys_role-通过id删除")
    @ApiOperation(value = "sys_role-通过id删除")
    //@RequiresPermissions("role:SysRole:delete")
    @DeleteMapping(value = "/delete")
    public HttpResult<?> delete(@RequestParam("id") String id) {
        sysRoleService.removeById(id);
        return HttpResult.ok(I18nUtil.message("delete.success"));
    }

    /**
     * 通过id批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "sys_role-批量删除")
    @ApiOperation(value = "sys_role-批量删除")
    //@RequiresPermissions("role:SysRole:delete")
    @DeleteMapping(value = "/deleteBatch")
    public HttpResult<?> deleteBatch(@RequestParam("ids") String ids) {
        sysRoleService.removeByIds(Arrays.asList(ids.split(",")));
        return HttpResult.ok(I18nUtil.message("delete.success"));
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "sys_role-通过id查询")
    @ApiOperation(value = "sys_role-通过id查询")
    @GetMapping(value = "/queryById")
    public HttpResult<?> queryById(@RequestParam("id") String id) {
        OaSysRole oaSysRole = sysRoleService.getById(id);
        return HttpResult.ok(oaSysRole);
    }

    /**
     * 导出excel
     */
    @AutoLog(value = "sys_role-导出excel")
    @ApiOperation(value = "sys_role-导出excel")
    //@RequiresPermissions("role:SysRole:export")
    @RequestMapping(value = "/exportExcel")
    public ModelAndView exportExcel(HttpServletRequest request,  OaSysRole oaSysRole) {
    	return super.exportExcel(request, oaSysRole, OaSysRole.class, "sys_role");
    }

    /**
     * excel导入数据
     */
    @AutoLog(value = "sys_role-excel导入数据")
    @ApiOperation(value = "sys_role-excel导入数据")
    //@RequiresPermissions("role:SysRole:import")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public HttpResult<?> importExcel(HttpServletRequest request) {
    		return super.importExcel(request, OaSysRole.class);
    }

    /**
     * 数据转换
     * @param oaSysRole
     */
    private void formatValue(OaSysRole oaSysRole){
    }
}
